package cn.itcast.scala.course04

/**
  * 继承和重写
  * create by zhangtian on 2018/12/29
  */
object ExtendOverrideApp {
  def main(args: Array[String]): Unit = {
    var student = new Student("水梦星光", 28, "Math")
    println(student.name + " : " +student.age + " : " +student.major)

    println("toString override : " +student)
  }

  /**
    * 在class上的构造器为主
    * @param name
    * @param age
    */
  class Person(val name: String, val age: Int) {
    println("Person constructor enter ...")
    var school = "剑桥"
    var gender: String = _

    // 附属构造器
    def this (name: String, age: Int, gender: String){
      this(name, age) // 附属构造器的第一行代码必须要调用主构造器或其他构造器
      this.gender = gender
    }

    println("Person constructor leave ...")
  }

  /**
    * 子类继承父类时，父类构造有的变量不需要使用var/val修饰，子类特有的必须要使用var/val修饰，否则对戏那个找不到该属性
    * 执行时先执行父类的构造器，在执行子类的构造器
    * @param name
    * @param age
    * @param major
    */
  class Student(name: String ,age: Int, val major: String) extends Person(name: String, age: Int){
    println("Student constructor enter ...")
    var school1 = "华中科技"

    override def toString: String = "Student override ... " + school1

    println("Student constructor enter ...")
  }
}
